package com.winning.sx.microframework.model.genericapi;

import com.winning.sx.microframework.common.constant.GenericApiConstant;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.dao.EasyDao;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.service.EasyService;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Data
public class GenericApiInterfaceConfig {

    private Integer id;
    private Integer interfaceId;
    private Integer higherConfigId;
    private String name;
    private String code;
    private String type;
    private String controllability;
    private String maxLength;
    private Date createTime;
    private Date updateTime;
    private String state;
    private String validFlag;

    public Object valid(Map<String, Object> map) throws CustomException {
        String o = (String) map.get(getCode());

//        Class clz = null;
//        try {
//            clz = Class.forName(getType());
//        } catch (ClassNotFoundException e) {
//            throw new CustomException(-200,getName()+" 参数配置的类型不存在！");
//        }
//        boolean instance = clz.isInstance(o);
//        try{
//            if(instance){
//                clz.cast(o);
//            }else{
//                throw new CustomException(-200,getName()+"参数传入的类型不正确！");
//            }
//        }catch (Exception e){
//            throw new CustomException(-200,getName()+"参数传入的类型不正确！");
//        }
//        if(controllability()){
//            boolean isNull = false;
//            switch (getType()){
//                case "java.lang.String":
//                    isNull = StringUtils.isEmpty((String) o);
//                    break;
//                case "java.lang.Integer":
//                    isNull = ((int)o==0);
//                    break;
//            }
//            if (isNull) throw new CustomException(GenericApiConstant.ERROR_CODE_VALID_FAIL,getName() + " 参数不能为空！") ;
//        }
        if (controllability()) {
            if (StringUtils.isEmpty(o))
                throw new CustomException(GenericApiConstant.ERROR_CODE_VALID_FAIL, getName() + " 参数不能为空！");
            if (maxLength != null && !"0".equals(maxLength)&& !"".equals(maxLength))
                if (o.length() > Integer.parseInt(maxLength))
                    throw new CustomException(GenericApiConstant.ERROR_CODE_VALID_FAIL, getName() + " 参数长度过长！");
        } else {
            if (o != null)
                if (maxLength != null && !"0".equals(maxLength)&& !"".equals(maxLength))
                    if (o.length() > Integer.parseInt(maxLength))
                        throw new CustomException(GenericApiConstant.ERROR_CODE_VALID_FAIL, getName() + " 参数长度过长！");
        }

        return o;
    }


    private boolean controllability() {
        if (StringUtils.equals(Constains.INTER_NOT_ALLOW_NULL, getControllability())) return true;
        return false;
    }
}
